<!--
 * @Author: ZhaoZhiqi
 * @Date: 2022-08-09 09:47:21
 * @LastEditors: Kevin Zhao
 * @LastEditTime: 2022-12-13 21:27:45
 * @Description: 文章管理
 * @FilePath: \tf-admin\src\pages\Agency\index.vue
-->
<script setup lang="ts">
import moment from "moment";
import { Search, Plus } from "@element-plus/icons-vue";
import SearchItem from "../../components/SearchItem.vue";
import TableCell from "../../components/TableCell.vue";
import EditDrawer from "../../components/EditDrawer.vue";
import LogDialog from "../../components/LogDialog.vue";
import { ElMessage } from "element-plus";
import { updateOpenStatusAPI, deleteAgentAPI } from "../../api/feedback";
import { ref, reactive } from "vue";
import useTable from "./useTable";
import useForm from "./useForm";
import { useRoute } from "vue-router";
import { useRoleStore } from "../../store/role.js";

const path = useRoute().path;
const permissions = useRoleStore().permissions;
const permission = permissions[path] || {};
console.log("permission", permission);

const logVisible = ref(false);
// 查询条件
let filters = reactive({
    pageNo: 1,
    pageSize: 10,
});

// 列表数据
const { tableData, loading, totalCount, query } = useTable(filters);

// 表单
const {
    dialogVisible,
    formRef,
    isEdit,
    form,
    deptsList,
    handleEdit,
    handleConfirm,
} = useForm(query);

// 公开
const handleOpen = (row) => {
    let { id, isOpen } = row;
    updateOpenStatusAPI({
        id,
        isOpen: !isOpen,
    }).then((res) => {
        if (res.code === 200) {
            ElMessage({
                message: "更新成功",
                type: "success",
            });
            query();
        }
    });
};

// 删除
const handleDelete = (id: string) => {
    deleteAgentAPI({
        id,
    }).then((res) => {
        if (res.code === 200) {
            ElMessage({
                message: "删除成功",
                type: "success",
            });
            query();
        }
    });
};

const rules = reactive({
    acceptDeptName: [
        { required: true, message: "请输入受理部门", trigger: "blur" },
    ],
    reply: [
        { required: true, message: "请输入回复内容", trigger: "blur" },
        { min: 1, max: 500, message: "长度不超过500个字", trigger: "blur" },
    ],
});
</script>

<template>
    <div>
        <el-card class="search-wrapper common-card">
            <el-row :gutter="24">
            </el-row>
            <div class="search-button-wrapper">
                <el-button type="primary" :icon="Search" auto-insert-space @click="query">查询</el-button>
            </div>
        </el-card>

        <!-- Table -->
        <el-card class="common-card" style="width: 100%">
            <!-- 列表 -->
            <el-table v-loading="loading" :data="tableData" stripe width="1500px">
                <el-table-column prop="id" label="id" width="80" />
                <el-table-column prop="itemName" label="事项名称" />
                <el-table-column prop="fullName" label="姓名" />
                <el-table-column prop="phone" label="手机号" />
                <el-table-column prop="chargeDept" label="负责部门" />
                <el-table-column prop="town" label="所属乡镇" />
                <el-table-column prop="content" label="内容留言" />
                <el-table-column prop="createDate" label="提交时间" width="200" />
                <el-table-column prop="fileList" label="附件" width="300">
                    <template #default="scope">
                        <el-link v-for="(item, index) in scope.row.fileList" :key="index" type="primary" :href="item.url" target="_black">{{item.name}}{{(index + 1) == scope.row.fileList.length ? '' : ','}}</el-link>
                    </template>
                </el-table-column>

                <template #empty>
                    <el-empty description="暂无数据"></el-empty>
                </template>
            </el-table>
            <!-- 分页 -->
            <el-pagination style="margin-top: 20px; justify-content: flex-end" background layout="total, sizes, prev, pager, next" :total="totalCount" :default-current-page="1" v-model:current-page="filters.pageNo" v-model:page-size="filters.pageSize"></el-pagination>
            <div v-if="permission['操作日志']" style="text-align: center; margin-top: 10px">
                <el-button type="primary" link @click="logVisible = true">查看操作日志</el-button>
            </div>
        </el-card>

        <EditDrawer title="回复代办" :visible="dialogVisible" @update:visible="dialogVisible = $event" @confirm="handleConfirm">
            <el-form ref="formRef" :model="form" :rules="rules" label-width="80px">
                <el-form-item label="受理部门" prop="acceptDeptName">
                    <el-input style="width: 400px" v-model="form.acceptDeptName" placeholder="请输入受理部门"></el-input>
                </el-form-item>
                <el-form-item label="回复内容" prop="reply">
                    <el-input type="textarea" :rows="6" style="width: 400px" v-model="form.reply" placeholder="请输入回复内容"></el-input>
                </el-form-item>
            </el-form>
        </EditDrawer>

        <LogDialog :visible="logVisible" :path="path" @update:visible="logVisible = $event"></LogDialog>
    </div>
</template>

<style lang="less" scoped>
@import url("../../assets/less/common.less");
@import url("../../assets/less/search.less");

.form-item {
    font-size: 14px;
    line-height: 20px;
    color: @color_regular-text;
    margin-bottom: 20px;
    display: flex;
    .form-item-label {
        flex: none;
        width: 100px;
        text-align: right;
        padding-right: 12px;
        box-sizing: border-box;
    }
    .form-item-value {
        flex: none;
        width: 400px;
        padding-right: 20px;
        color: @color_primary-text;
    }
}
</style>